﻿Imports MeoTom.Core.BLL
Imports MeoTom.Core

Namespace Control

    Partial Public Class ArticleList
        Inherits System.Web.UI.UserControl
        Public objArticle As New BLL.Articles
        Public util2 As New Utilities.Common()

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim previousPage As String = ""
            If Request.QueryString.ToString = vbNullString Then
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
            Else
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
            End If
            util2.ValidateLogin(previousPage)

            BindGrid()
            DisplayCategory()
        End Sub

        Public Sub BindGrid()
            Select Case Request.QueryString("action")
                Case "getByCat"
                    objArticle.CategoryId = Int32.Parse(Request.QueryString("CatID"))
                    grdListArticle.DataSource = objArticle.GetByCatInside2().Tables(0).DefaultView
                    grdListArticle.DataBind()
                Case "getByTag"
                    objArticle.TagID = Int32.Parse(Request.QueryString("TagID"))
                    grdListArticle.DataSource = objArticle.GetByTagInside()
                    grdListArticle.DataBind()
                Case "Edit"
                Case Else
                    grdListArticle.DataSource = objArticle.GetAll()
                    grdListArticle.DataBind()
            End Select
        End Sub

        Public Function DisplayStatus(ByVal b As Boolean) As String
            Dim str As String = ""
            If Not b Then
                str = " - <b>Draft</b>"
            End If
            Return str
        End Function

        ''' <summary>Display Title and Cut if too long</summary>
        ''' <param name="Title">String Title to display</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function DisplayTitle(ByVal Title As String) As String
            Dim strOut As String
            If Len(Title) > 62 Then
                strOut = Left(Title, 62) & "..."
            Else
                strOut = Title
            End If
            Return strOut
        End Function

        Public Function DisplayStatusDate(ByVal b As Boolean, ByVal d As String)
            Dim str As String = ""
            Dim dDate As DateTime = DateTime.Parse(d).AddHours(Configs.TimeLine)
            If Not b Then
                str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Draft"
            Else
                If dDate > DateTime.Now.AddHours(Configs.TimeLine) Then
                    str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Scheduled"
                Else
                    str = String.Format("{0:dd/MM/yyyy}", dDate) & "<br />Published"
                End If
            End If
            Return str
        End Function

        Public Function DisplayCategory(ByVal ArticleId As String) As String
            objArticle.ArticlesID = Integer.Parse(ArticleId)
            Return objArticle.GetAllCat(0)
        End Function

        Public Function DisplayTag(ByVal ArticleId As String) As String
            objArticle.ArticlesID = Integer.Parse(ArticleId)
            Dim strOut As String = objArticle.GetAllTag(0)
            If strOut.Length < 1 Then
                strOut = "No Tags"
            End If
            Return strOut
        End Function

        Public Sub DisplayCategory()
            Dim objCategory As New Categories()
            mycat.InnerHtml = objCategory.WriteCategoryInside(4, Request.QueryString("CatID"))
            objCategory = Nothing
        End Sub

        Private Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
            If Request.Form("cboCategory") = "0" Then
                Response.Redirect("/mt-inside/ArticleUpdate.aspx", False)
            Else
                Response.Redirect("/mt-inside/ArticleUpdate.aspx?action=getByCat&CatID=" & Request.Form("cboCategory"), False)
            End If
        End Sub

        Private Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
            Dim objTag As New Tags()
            Try
                Const i As Integer = 0
                If Request.Form("selectedAction0") = "1" Or Request.Form("selectedAction1") = "1" Then
                    'Edit Category
                    Dim vId As String = ""
                    For Each str As String In From str1 In Regex.Split(Request.Form("cboCheckArticle"), ",") Where i = 0
                        vId = str
                        If Not IsNumeric(vId) Then
                            vId = "1"
                        End If
                        Exit For
                    Next
                    Response.Redirect("/mt-inside/ArticleUpdate.aspx?action=Edit&ID=" & vId, False)
                ElseIf Request.Form("selectedAction0") = "2" Or Request.Form("selectedAction1") = "2" Then
                    'Delete
                    For Each str As String In Regex.Split(Request.Form("cboCheckArticle"), ",")
                        DeleteArticle(Integer.Parse(str))
                    Next
                    objTag.GetDefaultTag(0)
                    objArticle.WriteTopStory()
                    objArticle.WriteTop10View()
                    objArticle.WriteTop10Last()
                    BindGrid()
                End If
            Catch ex As Exception

            Finally
                objTag = Nothing
            End Try
        End Sub

        Private Sub btnApply2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply2.Click
            btnApply_Click(sender, e)
        End Sub

        Public Sub DeleteArticle(ByVal ArticleID As Integer)
            Dim objTag As New Tags()
            Dim objComment As New Comments()
            Try
                objArticle.ArticlesID = ArticleID
                objArticle.GetByEdit()

                objTag.ArticleID = ArticleID
                objTag.UpdateTotalArticle()

                objComment.ArticleId = objArticle.ArticlesID
                objComment.DeleteByArticle()

                util2.DeleteFile(Configs.ArticleFile & "ListFile\" & objArticle.CreatedDate.Year.ToString() & "\" & objArticle.CreatedDate.Month.ToString() & "\" & objArticle.CreatedDate.Day.ToString() & "\" & objArticle.ArticlesID & ".xml")

                objArticle.ArticlesID = ArticleID
                objArticle.Delete()
            Catch ex As Exception

            Finally
                objComment = Nothing
                objTag = Nothing
            End Try
        End Sub

        Private Sub grdListArticle_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdListArticle.PageIndexChanging
            grdListArticle.PageIndex = e.NewPageIndex
            BindGrid()
        End Sub
    End Class
End Namespace